package org.jordens.jdbcspy;

import javax.management.remote.*;

/**
 * Utility methods for creating and maintaining JMX connections.
 *
 * @author Adam Jordens
 */
public class JMXUtilities
{
    /**
     * Attempt to open a JMXConnector to the specified host and port.
     *
     * @param host Host name
     * @param port Port
     * @return JMXConnector or null if unable to connect
     */
    public static JMXConnector getJMXConnector(String host, int port)
    {
        try
        {
            return JMXConnectorFactory.connect(
                    new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi")
            );
        }
        catch (Exception e)
        {
            System.err.println("Unable to connect to " + host + ":" + port);
            e.printStackTrace(System.err);
        }

        return null;
    }
}
